<?php

namespace beObject;

use beObject\Primitive\String;
use beObject\Utils\ArrayList;
use beObject\Exceptions\IllegalArgumentException;

/**
 * This class provides generic methods for a simple object.<br />
 * It can be assimilated to the Object class in other languages.<br />
 * @author Dany Mottier
 * @version 2.3.0
 * @copyright http://www.md-consult.be
 * @license http://www.gnu.org/copyleft/gpl.html
 * 
 * @package beObject
 * @uses beObject\Primitive\String
 * @uses beObject\Utils\ArrayList
 * @uses beObject\Exceptions\IllegalArgumentException
 * @abstract
 * @link http://localhost/beobject-framework.net/beobject/object/class/<br />
 */
abstract class Object {

  /**
   * Returns a human readable formatted object.
   * @param bool $output Set to <b>false</b> to avoid direct output.
   * @return String Returns a human readable formatted obejct.
   * @link http://localhost/beobject-framework.net/beobject/object/dump/
   * @link http://be2.php.net/manual/en/function.var-dump.php
   * @link http://be2.php.net/manual/en/function.print-r.php
   */
  public function dump($output = true) {
    if (is_bool($output) && $output) {
      var_dump($this);
    } else {
      throw new IllegalArgumentException("Parameter should be a boolean");
    }
    return new String(print_r($this,
                              true));
  }

  /**
   * Gets the class methods name.
   * @return ArrayList Returns an array of methods name of the current object.
   * @link http://localhost/beobject-framework.net/beobject/object/getmethods/
   * @link http://be2.php.net/manual/en/function.get-class-methods.php
   * @link http://be.php.net/manual/en/function.is-null.php
   */
  public function getMethods() {
    $result = get_class_methods($this);

    return !is_null($result) ? new ArrayList($result) : new ArrayList();
  }

  /**
   * Retrieves the parent class name.
   * @return String Returns the name of the parent class of the current class.
   * @link http://localhost/beobject-framework.net/beobject/object/getparentname/
   * @link http://be2.php.net/manual/en/function.get-parent-class.php
   */
  public function getParentName() {
    return new String(get_parent_class($this));
  }

  /**
   * Returns the type of the current object.
   * @return String The type of the current object.
   * @link http://localhost/beobject-framework.net/beobject/object/gettype/
   * @link http://be.php.net/manual/en/function.gettype.php
   */
  public function getType() {
    return new String(get_class($this));
  }

  /**
   * Gets the properties of the current object.
   * @return ArrayList Returns an arraylist of objects accessible non-static
   * properties for the current object in scope.<br />
   * If a property have not been assigned a value, it will be returned with a
   * null value.
   * @link http://localhost/beobject-framework.net/beobject/object/getvariables/
   * @link http://be2.php.net/manual/en/function.get-object-vars.php
   */
  public function getVariables() {
    $result = get_object_vars($this);

    return !is_null($result) ? new ArrayList($result) : new ArrayList();
  }

  /**
   * Checks if the object is of this class or has this class as one of its
   * parents.
   * @param String $classname The class name.
   * @return bool Returns <b>true</b> if the object is of this class or has 
   * this class as one of its parents, <b>false</b> otherwise.
   * @link http://localhost/beobject-framework.net/beobject/object/isa/
   * @link http://be2.php.net/manual/en/function.is-a.php
   */
  public function isA(String $classname) {
    return is_a($this,
                $classname->toString());
  }

  public function toString() {
    return new String(serialize($this));
  }

}

?>
